package fr.pascal.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.web.bindery.event.shared.EventBus;

import fr.pascal.client.mvp.AppActivityMapper;
import fr.pascal.client.mvp.AppPlaceHistoryMapper;
import fr.pascal.client.place.ChatPlace;
import fr.pascal.client.ui.ApplicationView;
import fr.pascal.client.ui.ApplicationViewImpl;

public class Gwt_01 implements EntryPoint {

	@Override
	public void onModuleLoad() {
		
		ApplicationView applicationView = new ApplicationViewImpl();
		RootPanel.get("applicationView").add(applicationView);

		Place defaultPlace = new ChatPlace("garfield");

		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
		activityManager.setDisplay(applicationView.getAnimalPanel());
		
		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		// Goes to the place represented on URL else default place
		historyHandler.handleCurrentHistory();
	}
}
